package com.hmall.item.Listener;

import com.hmall.item.service.IItemService;
import com.hmall.item.service.impl.ItemService;
import com.hmall.order.OrderDto;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 监听失效订单
 */
@Component
public class ItemListener {

    @Autowired(required = false)
    private IItemService itemService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.order.sub"),
            exchange = @Exchange(name = "item.order", type = ExchangeTypes.DIRECT),
            key = "sub"
    ))
    public void subItemStock(OrderDto orderDto) {
        //客户下单，减库存
        itemService.subItemStock(orderDto.getNum(), orderDto.getItemId());
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "item.order.recover"),
            exchange = @Exchange(name = "item.order", type = ExchangeTypes.DIRECT),
            key = "recover"
    ))
    public void recoverItemStock(OrderDto orderDto) {
        //客户未支付，回滚库存信息
        itemService.recoverItemStock(orderDto.getNum(), orderDto.getItemId());
    }

}